package com.yocard.yxyd.ui.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.yocard.yxyd.R;

/**
 * Created on 2019/4/4.
 */
public class GuideViewBg extends View {
    private int showX = 550;
    private int showY = 1250;
    private int radius = 50;
    private int circle_color = Color.TRANSPARENT;
    private Paint paint;
    private Bitmap bitmap;
    private int bitmapHeight;
    private int bitmapWidth;
    private Rect srcRect;
    private Rect dstRect;
    private int bg_color = Color.parseColor("#99000000");

    public GuideViewBg(@NonNull Context context) {
        this(context,null);
    }

    public GuideViewBg(@NonNull Context context, @NonNull AttributeSet attrs) {
        this(context, attrs,0);
    }

    public GuideViewBg(@NonNull Context context, @NonNull AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.bg_guide);
        paint = new Paint();
        paint.setAntiAlias(true);
        radius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 42,getContext().getResources().getDisplayMetrics());
        bitmapHeight = bitmap.getHeight();
        bitmapWidth = bitmap.getWidth();
        srcRect = new Rect();
        dstRect = new Rect();
        dstRect.left = 0;
        dstRect.top = 0;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int canvasWidth = getWidth();
        int canvasHeight = getHeight();
        int layerId = canvas.saveLayer(0, 0, canvasWidth, canvasHeight, null, Canvas.ALL_SAVE_FLAG);
        int r = canvasWidth / 3;
        //正常绘制黄色的圆形
        paint.setColor(bg_color);
        canvas.drawRect(0, 0, canvasWidth, canvasHeight, paint);
        //使用CLEAR作为PorterDuffXfermode绘制蓝色的矩形
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        paint.setColor(circle_color);
        canvas.drawCircle(showX, showY, radius, paint);
        //最后将画笔去除Xfermode
        paint.setXfermode(null);
        canvas.restoreToCount(layerId);

//        showX = getWidth()/2;
//        showY = getHeight()/2 + showY;
        srcRect.left = showX/2 - radius/2;
//        srcRect.left = 0;
        int top = showY - radius/2 - bitmapHeight;
        srcRect.top = top;
        srcRect.right = srcRect.left + bitmapWidth;
        srcRect.bottom = top + bitmapHeight;
        dstRect.right = dstRect.left + bitmapWidth;
        dstRect.bottom = dstRect.top + bitmapHeight;
        canvas.drawBitmap(bitmap, null, srcRect, null);
        printf(srcRect);
//        printf(dstRect);
//        canvas.drawBitmap(bitmap, 80, top, paint);
    }

    public void setShowX(int showX) {
        this.showX = showX;
        postInvalidate();
    }

    public void setShowY(int showY) {
        this.showY = showY;
        invalidate();
    }

    public void setRadius(int radius) {
        this.radius = radius;
        postInvalidate();
    }

    void printf(Rect rect) {

        System.out.println(rect + " left = " + rect.left + " top = " +  rect.top  + " right = " +   rect.right + " bottom = " +  rect.bottom);
        System.out.println(" showY = " + showY);
    }
}
